<?php

declare(strict_types=1);

namespace Plugin\Attractions\Inspection\Model\Inspection_device_types;

use App\Exception\BusinessException;
use App\Http\Common\ResultCode;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\DbConnection\Model\Model;
use Plugin\Attractions\Inspection\Constant;
use Plugin\Attractions\Inspection\Model\Inspection_devices\InspectionDevices;

/**
 * @property int $id ID
 * @property string $name 类型名称（游乐设备、广播设备、监控设备、其他设备）
 */
class InspectionDeviceTypes extends Model
{
    public bool $timestamps = false;
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'inspection_device_types';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'name'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer'];

    public function devices(): HasMany
    {
        return $this->hasMany(InspectionDevices::class, 'type_id', 'id');
    }

    public function updating(\Hyperf\Database\Model\Events\Updating $event)
    {
        if (in_array($this->id, array_column(Constant::DEFAULT_DEVICE_TYPES, 'id'))) {
            throw new BusinessException(ResultCode::NOT_ACCEPTABLE, '默认类型不允许修改');
        }
    }
}
